#include "ThreadPool.hpp"
#include <cstdlib>
#include <ctime>

#define  QUEUE_TASK_NUM 10     // 任务队列最大数

int main()
{
    ThreadPool<Task_add*>* st = ThreadPool<Task_add*>::Get_Instance(); //加载
    // 主函数就负责生产任务,向任务队列输入任务
    // 未来可能是网络端获取任务
    st->Run();
    srand(time(0) * 131 + 1);
    while (1)
    {   
        if (st->Get_queue_task_reserver_size() < QUEUE_TASK_NUM)
        {
            int x = rand() / 20 + 1;
            usleep(7777);   // 防止随机数重复吧
            int y = rand() / 30 + 4;
            // std::cout << x << " " << y << std::endl;

            Task_add* tk = new Task_add (x, y,  [](int a, int b){ 
            return a + b;
            });

            // std::cout << &tk << std::endl;
            st->push(tk);
        }

        // sleep(1);   
        // std::cout << "备用队列：" << st->Get_queue_task_reserver_size() << std::endl;
        // std::cout << "运行队列：" << st->Get_queue_task_size() << std::endl;

        if (st->Get_queue_task_reserver_size() >= QUEUE_TASK_NUM / 2  &&
            st->Get_queue_task_size() == 0 &&
            st->Get_queue_task_reserver_size() <= QUEUE_TASK_NUM)
            {
                st->swap_queue();
            }
    }

    return 0;
}


